//
//  CMessageDraftMgr.m
//  wanzhaoIM
//
//  Created by wanzhao on 15/9/11.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CMessageDraftMgr.h"
#import "CMessageDraft.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"


@implementation CMessageDraftMgr

- (BOOL)insertMessageDraft:(CMessageDraft *)messageDraft
{
    if(messageDraft == nil)
    {
        return NO;
    } else {
        if (M_ISSTRNULL(messageDraft.MineUserId) || M_ISSTRNULL(messageDraft.FriendUserId)) {
            return NO;
        }
    }
    
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdateWithFormat:@"INSERT INTO MESSAGEDRAFT(MessageContent,MineUserId,FriendUserId,MessageType,MessageSendedStatus,GroupId,TimeStamp,FriendUserName,MineUserName,IsShowInmessageList,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@)",  M_STOREVALUE(messageDraft.MessageContent) ,M_STOREVALUE(messageDraft.MineUserId),M_STOREVALUE(messageDraft.FriendUserId),M_STOREVALUE(messageDraft.MessageType),@(messageDraft.MessageSendedStatus),M_STOREVALUE(messageDraft.GroupId),M_STOREVALUE(messageDraft.TimeStamp),M_STOREVALUE(messageDraft.FriendUserName),M_STOREVALUE(messageDraft.MineUserName),messageDraft.IsShowInmessageList?@1:@0,M_STOREVALUE(messageDraft.yuLiuZiduan1),@(messageDraft.yuLiuZiduan2),M_STOREVALUE(messageDraft.yuLiuZiduan3)];
    }];
    return ret;

}

- (CMessageDraft *)loadMessageDraftWithFriend:(NSString *)friendId mineId:(NSString *)mineId
{
    if(M_ISSTRNULL(friendId) || M_ISSTRNULL(mineId))
    {
        return nil;
    }
    NSString *sql = nil;
    __block CMessageDraft *messageDraft = nil;
    sql = [NSString stringWithFormat:@"select * from MESSAGEDRAFT where  MineUserId = \'%@\' AND FriendUserId = \'%@\' AND IsShowInmessageList = 1", mineId,friendId];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            messageDraft = [[CMessageDraft alloc] init];
            messageDraft.MessageIndex = [result  longLongIntForColumn:@"MessageIndex"];
            messageDraft.MineUserId = [result stringForColumn:@"MineUserId"];
            messageDraft.MessageContent = [result stringForColumn:@"MessageContent"];
            messageDraft.FriendUserId = [result stringForColumn:@"FriendUserId"];
            messageDraft.MessageType = [result stringForColumn:@"MessageType"];
            messageDraft.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            messageDraft.GroupId = [result stringForColumn:@"GroupId"];
            messageDraft.TimeStamp = [result stringForColumn:@"TimeStamp"];
            messageDraft.FriendUserName = [result stringForColumn:@"FriendUserName"];
            messageDraft.MineUserName = [result stringForColumn:@"MineUserName"];
            messageDraft.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            
            messageDraft.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            messageDraft.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            messageDraft.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

        }
        //        CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
        //        contact.DEPNAME = dep.DepartName;
      [result close];
    }];
    return messageDraft;

}

- (NSArray *)loadAllmessageDraft
{
    NSString *sql = nil;
    __block NSMutableArray *arrayM = [NSMutableArray array];
    sql = [NSString stringWithFormat:@"select * from MESSAGEDRAFT where IsShowInmessageList = 1"];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
           CMessageDraft *messageDraft = [[CMessageDraft alloc] init];
            messageDraft.MessageIndex = [result  longLongIntForColumn:@"MessageIndex"];
            messageDraft.MineUserId = [result stringForColumn:@"MineUserId"];
            messageDraft.MessageContent = [result stringForColumn:@"MessageContent"];
            messageDraft.FriendUserId = [result stringForColumn:@"FriendUserId"];
            messageDraft.MessageType = [result stringForColumn:@"MessageType"];
            messageDraft.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            messageDraft.GroupId = [result stringForColumn:@"GroupId"];
            messageDraft.TimeStamp = [result stringForColumn:@"TimeStamp"];
            messageDraft.FriendUserName = [result stringForColumn:@"FriendUserName"];
            messageDraft.MineUserName = [result stringForColumn:@"MineUserName"];
             messageDraft.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            
            messageDraft.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            messageDraft.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            messageDraft.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            [arrayM addObject:messageDraft];
            messageDraft = nil;
        }
        //        CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
        //        contact.DEPNAME = dep.DepartName;
       [result close];
    }];
    
    if (arrayM.count > 0) {
        return arrayM;
    } else {
        return nil;
    }
}

- (BOOL)updateMessageDraftWithMessageDraft:(CMessageDraft *)messageDraft
{
    if(messageDraft == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE MESSAGEDRAFT SET MessageContent = ?,MessageType = ?,MessageSendedStatus = ?,GroupId = ?,TimeStamp = ?,FriendUserName = ?,MineUserName = ?,IsShowInmessageList = ? where MineUserId = ? AND FriendUserId = ?", M_STOREVALUE(messageDraft.MessageContent) ,M_STOREVALUE(messageDraft.MessageType),@(messageDraft.MessageSendedStatus),M_STOREVALUE(messageDraft.GroupId),M_STOREVALUE(messageDraft.TimeStamp),M_STOREVALUE(messageDraft.FriendUserName),M_STOREVALUE(messageDraft.MineUserName),messageDraft.IsShowInmessageList?@1:@0,M_STOREVALUE(messageDraft.MineUserId),M_STOREVALUE(messageDraft.FriendUserId)];
        
    }];
    return ret;

}

- (BOOL)delMessageDraftWithFriend:(NSString *)friendId mineId:(NSString *)mineId
{
    if(M_ISSTRNULL(friendId) || M_ISSTRNULL(mineId))
    {
        return NO;
    }
     NSString *sql = nil;
    sql = [NSString stringWithFormat:@"delete from MESSAGEDRAFT where  MineUserId = \'%@\' AND FriendUserId = \'%@\'", mineId,friendId];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:sql];
    }];
    return ret;

}


- (BOOL)delAllMessageDraft
{
    NSString *sql = nil;
    sql = [NSString stringWithFormat:@"delete from MESSAGEDRAFT"];
//
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:sql];
    }];
    return ret;

}


- (BOOL)updateMessageDraftWithIsShowInMessageList:(BOOL)IsShowInMessageList
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE MESSAGEDRAFT SET IsShowInmessageList = ?",IsShowInMessageList?@1:@0];
        
    }];
    return ret;
}

- (BOOL)updateMessageWithIsShowInMessageList:(BOOL)IsShowInMessageList FriendId:(NSString *)FriendId MineId:(NSString *)MineId
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    if (M_ISSTRNULL(FriendId) || M_ISSTRNULL(MineId)) {
        return NO;
    }
    __block BOOL ret = NO;
    
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE MESSAGEDRAFT SET IsShowInmessageList = ?  where  MineUserId = ? AND FriendUserId = ?",IsShowInMessageList?@1:@0,MineId,FriendId];
        
    }];
    return ret;

}

- (NSArray *)loadAllmessageDraftGroupId
{
    NSString *sql = nil;
    __block NSString *groupId = nil;
    __block NSMutableArray *arrayM = [NSMutableArray array];
    sql = [NSString stringWithFormat:@"select GroupId from MESSAGEDRAFT"];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            groupId = [result stringForColumn:@"GroupId"];
            if (!M_ISSTRNULL(groupId)) {
                [arrayM addObject:groupId];
            }
            groupId = nil;
        }
        //        CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
        //        contact.DEPNAME = dep.DepartName;
        [result close];
    }];
    
    if (arrayM.count > 0) {
        return arrayM;
    } else {
        return nil;
    }

}

- (BOOL)delMessageDraftWithGroupId:(NSString *)GroupId
{
    if(M_ISSTRNULL(GroupId))
    {
        return NO;
    }
    NSString *sql = nil;
    sql = [NSString stringWithFormat:@"delete from MESSAGEDRAFT where GroupId = \'%@\'",GroupId];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:sql];
    }];
    return ret;

}
@end
